<?php
	header("Content-type: text/html;charset=utf8");
	require_once("../db/dbclass.php");
	require_once("../model/userinfo.php");
	require_once("../utils/common.php");
	$user_id=$_POST['user_id'];
	$type=$_POST['type'];
	$myDb=&DbController::getInstance();
	$friend_id="";
	switch($type){
		case "1":
				$friend_id=$_POST['param'];
				break;
		case "2":
				$friend_name=$_POST['param'];
				$param="select user_id from yj_user_info where user_name ='".$friend_name."'";
				$result = $myDb->exec_query($param);
				if($result == TRUE){
					while($row = mysqli_fetch_array($result,MYSQL_ASSOC)){
						$friend_id=$row['user_id'];
					}
				}
				break;
		case "3":
				$friend_email=$_POST['param'];
				$param="select user_id from yj_user_info where user_email ='".$friend_email."'";
				$result = $myDb->exec_query($param);
				if($result == TRUE){
					while($row = mysqli_fetch_array($result,MYSQL_ASSOC)){
						$friend_id=$row['user_id'];
					}
				}
				break;
	}
	if($friend_id == ""){
		$result='{"result":"failure"}';
	}
	else{
		$param="select * from yj_friend where user2_id ='".$user_id."' and user1_id ='".$friend_id."'";
		if($myDb->exec_query($param) == TRUE){
			$result=mysqli_num_rows($myDb->exec_query($param));
			echo $result;
			//还不是好友时添加好友，已是好友时删除好友
			switch ($result) {
				case '0'://数据库记录好友关系
					$param="INSERT INTO yj_friend (user1_id, user2_id,friend_time) VALUES ('".$friend_id."','".$user_id."','".date('y-m-d h:i:s',time())."')";
					$result=$myDb->exec_query($param);
					if($result==TRUE){
						//修改用户的关注数和粉丝数
						$param="UPDATE yj_user_info SET follow_num=follow_num+1 WHERE user_id = $friend_id";
						$result=$myDb->exec_query($param);
						if($result==TRUE){
							$param="UPDATE yj_user_info SET focus_num=focus_num+1 WHERE user_id = $user_id";
							$result=$myDb->exec_query($param);
							if($result==TRUE){
								$result='{"result":"success"}';
							}
							else{
								$result='{"result":"failure"}';
							}
						}
						else{
							$result='{"result":"failure"}';
						}	
					}
					else{
						$result='{"result":"failure"}';
					}
					break;
				default:
					$param="DELETE FROM yj_primsg WHERE sender_id='".$user_id."' AND receiver_id='".$friend_id."' OR sender_id='".$friend_id."' AND receiver_id='".$user_id."'";
					$result=$myDb->exec_query($param);
					if($result==TRUE){
						echo "success";
					}
					$param="DELETE FROM yj_friend WHERE user2_id='".$user_id."' AND user1_id='".$friend_id."'";
					$result=$myDb->exec_query($param);
					if($result==TRUE){
						$param="UPDATE yj_user_info SET follow_num=follow_num-1 WHERE user_id = $friend_id";
						$result=$myDb->exec_query($param);
						if($result==TRUE){
							$param="UPDATE yj_user_info SET focus_num=focus_num-1 WHERE user_id = $user_id";
							$result=$myDb->exec_query($param);
							if($result==TRUE){
								$result='{"result":"success"}';
							}
							else{
								$result='{"result":"failure3"}';
							}
						}
						else{
							$result='{"result":"failure1"}';
						}	
					}
					else{
						$result='{"result":"failure2"}';
					}
					break;
			}
		}
		else{
			$result='{"result":"failure"}';
		}
	}
	// json_encode($result);
	echo $result;
?>